package com.aliang.book_java1.po.读取文件夹下文件名;

import com.alibaba.fastjson.JSON;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class DirectoryScanner {

    static List<String> mavenLibList=new ArrayList<>();
    static List<String> serverLibList=new ArrayList<>();
    static List<String> server_mavenLibList=new ArrayList<>();


    public static void main(String[] args) {
        File directory = new File("E:\\NDXT\\workplace2\\jsfgw_szfg\\fileserver-web\\target\\fileserver-web-1.0-SNAPSHOT\\WEB-INF\\lib"); // 替换为你的文件夹路径

        listFilesRecursively(directory,mavenLibList);
        //System.out.println("-----mavenLib--"+ JSON.toJSONString(mavenLib));
        File directory2 = new File("C:\\Users\\Tony丝瓜壳\\Desktop\\2024.4.29-文件服务修改\\lib"); // 替换为你的文件夹路径

        listFilesRecursively(directory2,serverLibList);

        List<String> server_mavenLibList=contrastA_B(serverLibList,mavenLibList);
        List<String> maven_serverLibList=contrastA_B(mavenLibList,serverLibList);
        System.out.println("--mavenLibList--1----"+mavenLibList.size());
        System.out.println("--serverLibList--1----"+serverLibList.size());

        System.out.println("--server_mavenLibList------"+server_mavenLibList.size());
        System.out.println("--maven_serverLibList------"+maven_serverLibList.size());
        server_mavenLibList.stream().forEach(w->FileCopier
                .copyFile("C:\\Users\\Tony丝瓜壳\\Desktop\\2024.4.29-文件服务修改\\lib\\"+w,
                        "C:\\Users\\Tony丝瓜壳\\Desktop\\2024.4.29-文件服务修改\\diffLib"));
    }

    public static void listFilesRecursively(File directory,List<String> list) {
        if (directory.isDirectory()) {
            File[] files = directory.listFiles();
            if (files != null) {
                for (File file : files) {
                    if (file.isDirectory()) {
                        // 递归遍历子目录
                        listFilesRecursively(file,list);
                    } else {
                        // 打印文件名
                        //System.out.println("------"+file.getName());
                        list.add(file.getName());

                    }
                }
            }
        }
    }
    public static List<String> contrastA_B(List<String> a,List<String> b){
        List<String> newList=new ArrayList<>();
        Map<String,String> bMap=new HashMap<>();
        b.stream().forEach(w->bMap.put(w,"1"));
        a.stream().filter(k->!bMap.containsKey(k)).forEach(s->newList.add(s));
        return newList;
    }
}
